package org.swxjava.assembler.avm1.convert;

import org.swxjava.assembler.Converter;
import org.swxjava.assembler.ObjectCompiler;
import org.swxjava.assembler.avm1.AsBoolean;
import org.swxjava.assembler.avm1.AsDouble;
import org.swxjava.assembler.avm1.AsFloat;
import org.swxjava.assembler.avm1.AsInt;
import org.swxjava.assembler.avm1.AsLong;
import org.swxjava.assembler.avm1.AsString;

public class PrimitiveConverter extends BaseConverter implements Converter {

    public ObjectCompiler convert(Object object)
    {
        Class paramType = object.getClass();

        if (object.equals(Boolean.TRUE))
        {
            return AsBoolean.TRUE;
        }
        else if (object.equals(Boolean.FALSE))
        {
            return AsBoolean.FALSE;
        }
        else if (paramType.equals(byte.class) 
        		|| paramType.equals(Byte.class)
        		|| paramType.equals(int.class) 
        		|| paramType.equals(Integer.class)
        		|| paramType.equals(short.class) 
        		|| paramType.equals(Short.class))
        {
        	return new AsInt(((Number)object).intValue());
        }
        else if (paramType.equals(long.class) 
        		|| paramType.equals(Long.class))
        {
        	return new AsLong(((Number)object).longValue());
        }
        else if (paramType.equals(float.class) 
        		|| paramType.equals(Float.class))
        {
        	return new AsFloat(((Number)object).floatValue());
        }
        else if (paramType.equals(double.class) 
        		|| paramType.equals(Double.class))
        {
        	return new AsDouble(((Number)object).doubleValue());
        }
        else if (paramType.equals(char.class) 
        		|| paramType.equals(Character.class))
        {
            // Treat characters as strings
            return new AsString(String.valueOf(object));
        }
        else
        {
            // We just use the default toString for all numbers
        	return new AsString(object.toString());
        }
    }
    
}
